console.log('Script loaded');
$(document).ready(function() {
    console.log('Document ready');

    // 初始化模态框
    const successModal = new bootstrap.Modal(document.getElementById('successModal'));
    const errorModal = new bootstrap.Modal(document.getElementById('errorModal'));

    $('#saveConfig').on('click', function() {
        console.log('Save button clicked');

        // 更新每次抽题数
        $.ajax({
            url: '/admin/api/config',
            type: 'POST',
            data: JSON.stringify({
                key: 'questions_per_draw',
                value: $('#questions_per_draw').val()
            }),
            contentType: 'application/json',
            success: function(response) {
                console.log('Question count updated:', response);

                //更新重抽限制
                $.ajax({
                    url: '/admin/api/config',
                    type: 'POST',
                    data: JSON.stringify({
                        key: 'retry_limit',
                        value: $('#retry_limit').val()
                    }),
                    contentType: 'application/json',
                    success: function(response) {
                        successModal.show();
                    },
                    error: function(xhr) {
                        $('#errorMessage').text(xhr.responseJSON.error || '更新失败');
                        errorModal.show();
                    }
                });
            },
            error: function(xhr) {
                $('#errorMessage').text(xhr.responseJSON.error || '更新失败');
                errorModal.show();
            }
        });
    });
});